<?php
namespace Admin\Controller;
use Think\Controller;
class RelativeController extends GloableController {
      //更新数据
      public function update_relative()
      {

        $user_id=I('user_id');
        $this->assign("user_id",$user_id);
        // $user_info=M("user")->where("id='$id'")->find();

        // $phone=$user_info['phone'];

        $relative_info=M("relative")->alias('r')->join(' tp_user as u  ON r.user_id = u.id')->where("r.user_id='$user_id'")->find();


         if(!$relative_info)
         {

           $this->show("<script>alert('数据异常')</script>");exit();

         }


        if(IS_POST)
        {
           $level=$relative_info['level'];


            switch ($level) {
              case 1:
                $info=D("User")->add_user();
                break;
             case 2:
                $info=D("User")->add_user1();
                break;
             case 3:
                $info=D("User")->add_user2();
                break;
             case 4:
                $info=D("User")->add_user3();
                break;
              default:
                $info="等级有误";
                break;
            }

             $this->show($info);exit();
          
        }





         $this->assign('relative_info',$relative_info);
        //大区
         if($relative_info["level"]>1)
         {
           $data = M('relative')->distinct ( true )->order('offical asc')->field ( 'offical' )->select ();

            // $info = M('relative')->distinct ( true )->where("zoon!='00'")->order('zoon asc')->field ( 'zoon' )->select ();
            // foreach ($info as $k => $v) {
            //   if($v['zoon']==""){
            //     $info[$k]['zoon']='00';
            //   }
            // } 
            $this->assign("data",$data);

         }
         
         //总代理
         if($relative_info["level"]>2)
         {
            //官方对应大区
            $info=$this->get_zoon_byofficial($relative_info['offical'],2,$relative_info);
            $this->assign("info",$info);
         }
         //普通代理
         if($relative_info["level"]>3)
         {
            //官方对应大区
            $agent=$this->get_agent_byofficial($relative_info['offical'],$relative_info['zoon'],2,$relative_info);
            $this->assign("agent",$agent);
         }

        $this->display();
        
      }

    


       //按官方获取大区
   public function get_zoon_byofficial($offical="",$tp=1,$relative_info='')
   {
      if($_POST)
      {
         $data=$_POST;
         $offical=$data['offical']?$data['offical']:'';
      }

       $info = M('relative')->distinct ( true )->where("zoon!='00' and offical='$offical'")->order('zoon asc')->field ( 'zoon' )->select ();


       foreach ($info as $k => $v) {
          if($v['zoon']==""){
           $info[$k]['zoon']='00';
         }
       }

       $html="<select id='zoon_data' name='zoon' onchange='change_agent(this)'>";
       foreach ($info as $key => $value) {
         $html.="<option value='".$value['zoon']."'";
          if($value['zoon']==$relative_info['zoon'])
            $html.=" selected='selected' ";
          $html.=">".$value['zoon']."</option>";
       }
       $html.="</select>";
       if($tp==2)
       {
         return $html;exit();
       }


       $this->ajaxReturn($html,'json');
   }
   //按大区获取总代理
   public function get_agent_byofficial($offical="",$zoon='',$tp=1,$relative_info='')
   {
      
      if($_POST)
      {
         $data=$_POST;
         $zoon=$data['zoon']?$data['zoon']:'';
         $offical=$data['offical']?$data['offical']:'';
      }


       $info = M('relative')->distinct ( true )->where("agent!='00' and zoon='$zoon' and offical='$offical'")->order('agent asc')->field ( 'agent' )->select ();

       foreach ($info as $k => $v) {
          if($v['agent']==""){
           $info[$k]['agent']='00';
         }
       }

       $html="<select id='agent_data' name='agent'  >";
       foreach ($info as $key => $value) {
          $html.="<option value='".$value['agent']."'";
          if($value['agent']==$relative_info['agent'])
            $html.=" selected='selected' ";
          $html.=">".$value['agent']."</option>";
       }
       $html.="</select>";
       if($tp==2)
       {
         return $html;exit();
       }


       $this->ajaxReturn($html,'json');
   }

   //变革级别
   public function change_shouquan()
   {
       $tp=I('tp');
       $user_id=I('user_id');

       $result['errcode']=0;
       $result['mes']="返回不操作";
       if(!$user_id){ $result['errcode']=1;$result['mes']="授权人ID不存在"; $this->ajaxReturn($result); exit();};

       $is_down=0;
       if($tp>1)
       {
         $is_down=2;
       }

       $rv_info = M('relative')->field('phone,level,offical,zoon,agent,normal')->where("user_id='$user_id'")->find();

       $phone=$rv_info['phone'];unset($rv_info['phone']);

       if(!$rv_info){ $result['errcode']=1;$result['mes']="授权人不存在"; $this->ajaxReturn($result); exit();}

       $update_lift_log['org_shouquan']=$rv_info['offical'].$rv_info['zoon'].$rv_info['agent'].$rv_info['normal'];

       if($rv_info['level']==1&&!$is_down)
       {
           $result['mes']="已是最高级，无法再升级";
           $result['errcode']=1;

       }
       else if($rv_info['level']==4&&$is_down)
       {
          $result['mes']="已是最低级，无法再降级";
          $result['errcode']=1;

       }
       else if($rv_info['level'])
       {
          $level= intval($rv_info['level']);

          //降级
          if($is_down) $level++;
           //升级
           else  $level--;

           $rv_info['level']=$level;
        
           switch ($level) {
               case '4':
               //生成-代理
               $rv_info['normal']=D("Relative")->build_agent($rv_info['offical'],$rv_info['zoon'],$rv_info['agent']);

               break;

             case '3':
               $rv_info['normal']='';
       

               //生成-总代理
               $rv_info['agent']=D("Relative")->build_agent($rv_info['offical'],$rv_info['zoon']);

               break;
             case '2':
               $rv_info['normal']='';
               $rv_info['agent']='';


           
               //生成大区
               $rv_info['zoon']=D("Relative")->build_zoon($rv_info['offical']);
               break;
             case '1':
               $rv_info['normal']='';
               $rv_info['agent']='';
               $rv_info['zoon']='';

               
              //生成
               $rv_info['offical']=D("Relative")->build_offical();
               break; 
             
             default:
               $result['mes']="数据异常";
               $result['errcode']=1;
               break;    
         }

         $shouquan=$rv_info['offical'].$rv_info['zoon'].$rv_info['agent']. $rv_info['normal'];

         $status=M("relative")->where("user_id='$user_id'")->save($rv_info);
         

         //记录
          $update_lift_log['new_shouquan']= $shouquan;
          $update_lift_log['user_id']= $user_id;
          $update_lift_log['new_level']= $rv_info['level'];
          $update_lift_log['ctime']=time();
          $update_lift_log['status']=$is_down?$is_down:1;

          $status_log=M("user_lift_log")->add($update_lift_log);

          if($is_down)$mes=$phone.","."降级";
          else $mes=$phone.","."升级";

          if($status_log)
          {
            $result['mes']=$mes."成功";
          }
          else
            {$result['mes']=$mes."失败";}
       }
     


       $this->ajaxReturn($result);
   }
  

  

}
